﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class StationService : BaseService, IStationService
    {
        private readonly IWarehouseRepository _warehouseRepository;
        private readonly IStationRepository _stationRepository;
        private readonly IUnitOfWork _unitOfWork;
        public StationService(IMapper mapper, IStationRepository stationRepository, IWarehouseRepository _warehouseRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _stationRepository = stationRepository;
            _unitOfWork = unitOfWork;
            this._warehouseRepository = _warehouseRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddStation(Station obj, out string message)
        {
            message = "";
            var sobj = _stationRepository.QueryIsEntityWhere(x => x.Code == obj.Code);
            if (sobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if(obj.WhId<=0)
            {
                message = "请选择库房！";
                return false;
            }
            var wobj = _warehouseRepository.QueryEntityById(obj.WhId);
            if (wobj != null)
            {
                obj.WhName = wobj.Name;
                obj.WhCode = wobj.Code;
            }
            _stationRepository.AddEntity(obj);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelStation(long id)
        {
            var del =_stationRepository.QueryEntityById(id);
            if (del != null)
            {
               _stationRepository.DelEntity(del);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<Station> GetShippingOrderDetailStation(int type)
        {
            var slist = _stationRepository.GetEntityAlls();
            if (type > 0)
                slist = slist.Where(x => x.FunctionType == type);
            var list = slist.ToList();
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Station? GetStationById(long id)
        {
            return _stationRepository.QueryEntityById(id);
        }

        public List<Station> GetStationList(string? cname, long kid, int pageIndex, int pageSize, out int totalCount)
        {
            var alist = _stationRepository.GetEntityAlls(); ;
            var blist = _warehouseRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(cname))
            {
                alist = alist.Where(c => c.Name.Contains(cname));
            }
            if (kid > 0)
            {
                alist = alist.Where(x => x.WhId == kid);
            }
            var query = from A in alist
                        join B in blist
                        on A.WhId equals B.Id
                        select A;
            var list = _stationRepository.GetEntityAlls(query, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public bool UpdStation(Station obj, out string message)
        {
            message = "";
            var list = _stationRepository.QueryIsEntityWhere(x => x.Id != obj.Id && x.Code == obj.Code);

            if (list != null)
            {
                message = "编码已存在！";
                return false;
            }
            if (obj.WhId <= 0)
            {
                message = "请选择库房！";
                return false;
            }
            var wobj = _warehouseRepository.QueryEntityById(obj.WhId);
            if (wobj != null)
            {
                obj.WhName = wobj.Name;
                obj.WhCode = wobj.Code;
            }
            _stationRepository.UpdEntity(obj);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
